<?php
    /*
     * 优惠卷model
    */

    namespace app\listen\model;

    use think\db;
    use think\Exception;
    use think\Loader;
    use think\model;

    class Voice extends Base {

        public function detail($data = []) {
            try {
                if (isset($data)) {
                    $createSql = function () use ($data) {
                        $where = [];
                        if (isset($data['id'])) {
                            $where['v.id'] = $data['id'];
                        }
                        if (isset($data['uid'])) {
                            $where['v.uid'] = $data['uid'];
                        }
                        return $this->alias("v")->join('listen_member m', 'v.uid=m.uid', 'LEFT')->where($where);
                    };

                    $lists = $createSql()->field("v.*,m.nickname,m.head_photo,m.sex")
                        ->select();


                    return [
                        'list' => $lists
                    ];


                    return $lists;
                } else {
                    return [];
                }
            } catch (Exception $e) {
                return [];
            }

        }

        // 用户的记录列表
        public function userList($uid, $page = 0, $row = 10) {
            if (!isset($uid)) return false;
            $createSql = function () use ($uid) {
                $where = ['v.uid' => $uid];
                return $this->alias("v")
                    ->join('listen_member m', 'v.uid=m.uid', 'LEFT')
                    ->where($where);
            };

            try {
                $total = $createSql()->field('count(1)')->count();
                $lists = $createSql()
                    ->page($page, $row)
                    ->field("v.*,m.nickname,m.head_photo,m.sex")
                    ->order('v.addtime desc')
                    ->select();
                return [
                    'total'=>$total,
                    'list' => $lists
                ];
            } catch (Exception $e) {
                return false;
            }

        }

        // 首页的记录列表，读取审核通过的，以及用户自己的
        public function homeList($uid, $page, $row, $latitude, $longitude, $distance = 0.01) {
            $createSql = function () use ($uid, $longitude, $latitude, $distance) {
                $q = $this->alias("v")
                    ->join('listen_member m', 'v.uid=m.uid', 'LEFT');
                $orMap = ['v.audit_status' => 1];

                $q->where($orMap)->where(function (db\Query $sq) use ($orMap) {
                    if (!empty($uid)) {
                        $sq->whereOr(['v.uid' => $uid]);
                    }
                });
                $q->where([
                    'v.latitude' => [
                        ['gt', $latitude - $distance],
                        ['lt', $latitude + $distance]
                    ],
                    'v.longitude' => [
                        ['gt', $longitude - $distance],
                        ['lt', $longitude + $distance]
                    ],
                ]);
                return $q;
            };
            $createSql2 = function () use ($uid, $longitude, $latitude, $distance) {
                $q = $this->alias("v")
                    ->join('listen_member m', 'v.uid=m.uid', 'LEFT');
                $orMap = ['v.audit_status' => 1];

                $q->where($orMap)->where(function (db\Query $sq) use ($orMap) {
                    if (!empty($uid)) {
                        $sq->whereOr(['v.uid' => $uid]);
                    }
                });
                return $q;
            };
            try {


                $total = $createSql2()->field('count(1)')->count();
                $lists = $createSql()
                    ->page($page, $row)
                    ->field("v.*,m.nickname,m.head_photo,m.sex")
                    ->order(' ACOS(SIN((' . $latitude . ' * 3.1415) / 180 ) *SIN((v.latitude * 3.1415) / 180 ) +COS((' . $latitude . ' * 3.1415) / 180 ) * COS((v.latitude * 3.1415) / 180 ) *COS((' . $longitude . '* 3.1415) / 180 - (v.longitude * 3.1415) / 180 ) ) * 6380 asc')
                    ->select();
                return [
                    'total'=>$total,
                    'list' => $lists
                ];
            } catch (Exception $e) {
                return false;
            }
        }


        // 首页的记录列表，读取审核通过的，以及用户自己的
        public function myLove($uid, $page, $row) {
            if (!isset($uid)) return false;
            try {
                $createSql = function () use ($uid) {
                    $where = ['t.uid' => $uid];
                    return $this->alias("v")
                        ->join('listen_member m', 'v.uid=m.uid', 'LEFT')
                        ->join('listen_user_love t', 't.voice_id=v.id', 'LEFT')
                        ->where($where);
                };

                $total = $createSql()->field('count(1)')->count();
                $list = $createSql()->field("v.*,m.nickname,m.head_photo,m.sex")
                    ->page($page, $row)->order('v.addtime desc')->select();
                return [
                    'total'=>$total,
                    'list' => $list
                ];
            } catch (Exception $e) {
                return false;
            }

        }

        // 获取用户语音被喜欢的总数
        public function loveCount($uid) {
            if (!isset($uid)) return 0;
            try {
                $count = $this->query("SELECT count(1) FROM listen_voice v WHERE  EXISTS (( SELECT v.id FROM listen_user_love t WHERE  v.uid = " . $uid . "  AND t.voice_id = v.id GROUP BY v.id ))");
                if (!empty($count) && !empty($count[0]) && !empty($count[0]['count(1)'])) {

                    return $count[0]['count(1)'];
                }
                return 0;
            } catch (Exception $e) {
                print_r($e);
                return 0;
            }
        }

        public function update_voice($data = []) {
            if (isset($data['uid']) && isset($data['path'])) {
                return db('Voice')->insert($data, false, true);
            } else {
                return false;
            }
        }

        public function play($id) {
            try {
                return $this->where('id', $id)->setInc('play_number');
            } catch (Exception $e) {
                return false;
            }
        }
        public function share($id) {
            try {
                return $this->where('id', $id)->setInc('share_number');
            } catch (Exception $e) {
                return false;
            }
        }
        public function audit($id, $status) {
            try {
                return $this->where('id', $id)->update(['audit_status' => $status]);
            } catch (Exception $e) {
                return false;
            }
        }

        public function env($env) {
            $env = $env??'dev';
            try {
                $result = $this->table('setting')->where(['env'=>$env])->find();
                return $result;
            }catch (Exception $exception) {
                return ["isTest" => true ];
            }
        }
    }


